package com.atguigu.auth.controller;

import com.atguigu.auth.service.SysMenuService;
import com.atguigu.auth.service.SysUserService;
import com.atguigu.common.execption.GuiguException;
import com.atguigu.common.jwt.JwtHelper;
import com.atguigu.common.result.Result;
import com.atguigu.common.until.MD5;
import com.atguigu.model.system.SysMenu;
import com.atguigu.model.system.SysUser;
import com.atguigu.vo.system.LoginVo;
import com.atguigu.vo.system.RouterVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author CaiLu
 * @date 2023/3/8
 * @apiNote
 */
@Api(tags = "后台登录管理")
@RestController
@RequestMapping("/admin/system/index")
public class IndexController {

    @Autowired
    SysUserService sysUserService ;
    @Autowired
    SysMenuService sysMenuService ;
    /**
     * 登录
     * @return
     */
    @PostMapping("login")
    public Result login(@RequestBody LoginVo loginVo) {
//        Map<String, Object> map = new HashMap<>();
//        map.put("token","admin");
//        return Result.ok(map);
        //1.获取用户的用户名
        String username = loginVo.getUsername();
        //2.查询用户名是否存在
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser ::getUsername,username);
        SysUser sysUser = sysUserService.getOne(queryWrapper);
        if (sysUser == null) {
            throw new GuiguException(201,"用户不存在");
        }
        //3.判断用户名密码是否正确
        String encrypt = MD5.encrypt(loginVo.getPassword());
        String password_db = sysUser.getPassword();
        if(!encrypt.equals(password_db)){
            throw new GuiguException(201,"密码错误");
        }
        //4.判断用的当前状态
        //intValue()方法用于返回此Integer对象表示的值，该值转换为int类型(通过强制转换)。
        int status = sysUser.getStatus().intValue();
        if(status == 0){
            throw new GuiguException(201,"当前用户已被禁用");
        }
        //5.使用JWT根据用户id和用户名生成token返回
        String token = JwtHelper.createToken(sysUser.getId(), sysUser.getUsername());
        Map<String, Object> map = new HashMap<>();
        map.put("token",token);
        return Result.ok(map);
    }
    /**
     * 登录后获取用户信息
     * @return
     * HttpServletRequest 对象专门用于封装 HTTP 请求消息，简称 request 对象
     */
    @GetMapping("info")
    public Result info(HttpServletRequest request) {
        //1.获取前端发来的请求信息，将请求头中的token获取
        Long userId = JwtHelper.getUserId(request.getHeader("token"));
        //2.通过用户id来获取用户信息
        SysUser user = sysUserService.getById(userId);
        //3.通过用户id来操作数据库获取菜单信息列表
        List<RouterVo> routerList = sysMenuService.findUserMenuListByUserId(userId);
        //4.根据用户id操作按钮列表
        List<String> permsList = sysMenuService.findUserPermsByUserId(userId);
        //返回数据
        Map<String, Object> map = new HashMap<>();
        map.put("roles","[admin]");
        map.put("name",user.getUsername());
        map.put("avatar","https://oss.aliyuncs.com/aliyun_id_photo_bucket/default_handsome.jpg");
        //用户可操作的菜单按钮
        map.put("buttons", permsList);
        //用户可操作的菜单
        map.put("routers", routerList);
        return Result.ok(map);
    }
    /**
     * 退出
     * @return
     */
    @PostMapping("logout")
    public Result logout(){
        return Result.ok();
    }
}
